package org.dromara.pe.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.extern.slf4j.Slf4j;
import org.dromara.basic.domain.bo.MemberBo;
import org.dromara.basic.domain.bo.TeachClassBo;
import org.dromara.basic.domain.vo.MemberVo;
import org.dromara.basic.domain.vo.TeachClassVo;
import org.dromara.basic.service.IDictService;
import org.dromara.basic.service.IMemberService;
import org.dromara.basic.service.ITeachClassService;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidatorUtils;
import org.dromara.common.excel.core.ExcelListener;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.pe.domain.bo.DeviceScoreRawBo;
import org.dromara.pe.domain.vo.DeviceScoreRawImportVo;
import org.dromara.pe.service.IDeviceScoreRawService;
import org.dromara.run.service.IDeptService;
import org.dromara.run.service.IUserService;

import java.util.Date;
import java.util.List;

@Slf4j
public class DeviceScoreRawListener extends AnalysisEventListener<DeviceScoreRawImportVo> implements ExcelListener<DeviceScoreRawImportVo> {
    private final IDictService dictTypeService;
    private final IDeviceScoreRawService deviceScoreRawService;
    private final IMemberService memberService;
    private final IDeptService deptService;
    private final ITeachClassService teachClassService;
    private final Boolean isUpdateSupport;
    private final IUserService userService;
    private final Long operUserId;

    private int successNum = 0;
    private int failureNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public DeviceScoreRawListener(Boolean isUpdateSupport) {
        this.dictTypeService=SpringUtils.getBean(IDictService.class);
        this.memberService=SpringUtils.getBean(IMemberService.class);
        this.deviceScoreRawService = SpringUtils.getBean(IDeviceScoreRawService.class);
        this.deptService = SpringUtils.getBean(IDeptService.class);
        this.userService = SpringUtils.getBean(IUserService.class);
        this.teachClassService = SpringUtils.getBean(ITeachClassService.class);
        this.isUpdateSupport = isUpdateSupport;
        this.operUserId = LoginHelper.getUserId();
    }

    @Override
    public ExcelResult<DeviceScoreRawImportVo> getExcelResult() {
        return new ExcelResult<>() {

            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，用户详细信息导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，用户详细信息数据已全部导入成功！共 " + successNum + " 条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<DeviceScoreRawImportVo> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }

    @Override
    public void invoke(DeviceScoreRawImportVo deviceScoreRawImportVo, AnalysisContext analysisContext) {
        MemberBo memberBo=new MemberBo();
        memberBo.setIdNumber(deviceScoreRawImportVo.getUid());
        memberBo.setName(deviceScoreRawImportVo.getName());
        MemberVo memberVos = memberService.queryVo(memberBo);
        try {
            //验证是否有这个读者
            if (ObjectUtil.isNotNull(memberVos)) {
                DeviceScoreRawBo deviceScoreRawBo = BeanUtil.toBean(deviceScoreRawImportVo, DeviceScoreRawBo.class);
                deviceScoreRawBo.setGrade(memberVos.getGrade());
                LoginUser loginUser = LoginHelper.getLoginUser();
                String sportName = dictTypeService.getDictName("pe_project_name", deviceScoreRawImportVo.getSportId(),loginUser.getTenantId());
                deviceScoreRawBo.setCreateBy(operUserId);
                deviceScoreRawBo.setStationSn("人工");
                deviceScoreRawBo.setSportName(sportName);
                deviceScoreRawBo.setCompletedTime(new Date());
                deviceScoreRawService.insertByBo(deviceScoreRawBo);
                successNum++;
                successMsg.append("<br/>").append(successNum).append(deviceScoreRawBo.getName()).append("、体测原始成绩 ").append(" 导入成功");

            } else {
                failureNum++;
                failureMsg.append("<br/>").append(failureNum).append(deviceScoreRawImportVo.getName()).append("、原始成绩 ").append("导入失败").append("，原因：系统中无该名学生，或者学号姓名没对应");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum  + deviceScoreRawImportVo.getName() +"的体测原始成绩 "+ " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
